package com.ruoyi.web.controller.dm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.GpsTargetInfoDetailVO;
import com.ruoyi.dm.domain.GpsTargetInfoVO;
import com.ruoyi.dm.domain.GpsTargetParam;
import com.ruoyi.dm.service.IGpsTargetInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 数据采集-GPS目标属性
 */
@Slf4j
@RestController
@RequestMapping("/collect/gps")
@Api(tags = "数据采集-目标本体模型")
public class GpsTargetController extends BaseController {

    @Autowired
    private IGpsTargetInfoService gpsTargetService;

    /**
     * 检索
     * @param param
     * @return
     */
    @ApiOperation("数据查询")
    @PostMapping("/query")
    public R<IPage<GpsTargetInfoVO>> query(@ApiParam(name ="param",value="文件检索参数对象",required = false)@Validated @RequestBody GpsTargetParam param) {
        IPage<GpsTargetInfoVO> query = gpsTargetService.query(param);
        return R.ok(query);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @ApiOperation("数据详情")
    @GetMapping("/detail")
    public R<GpsTargetInfoDetailVO> detail(@ApiParam(name ="id",value="文件检索参数",required = true)@Validated @RequestParam Long id) {
        GpsTargetInfoDetailVO vo = gpsTargetService.queryDetail(id);
        return R.ok(vo);
    }

    /**
     * 导入
     * @param files
     * @return
     */
    @PostMapping ("/upload")
    @ApiOperation("数据导入")
    @ApiImplicitParam(value = "上传文件",name = "files",dataTypeClass = File.class,required = true)
    public R input(@RequestPart MultipartFile[] files) {
        try {
            String res = gpsTargetService.input(files);
            if (StringUtils.hasLength( res)) {
                return R.fail(res);
            }else {
                return R.ok();
            }

        }catch (Exception e) {
            log.error("",e);
            return R.fail("导入失败:"+e.getMessage());
        }
    }

    /**
     * 文件导出
     * @return
     */
    @ApiOperation("数据导出")
    @GetMapping("/export")
    public void   export(HttpServletResponse response) {
        try {
            String s = gpsTargetService.export();
            File file = new File(s);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
            file.delete();
        }catch (Exception e) {
            log.error("",e);
            log.error("下载失败",e);
        }
    }

    /**
     * 导出模板下载
     */
    @GetMapping("/export/model")
    @ApiOperation("导出模板下载")
    public void  modelExport(HttpServletResponse response) {
        try {
            String s = gpsTargetService.modelExport();
            File file = new File(s);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("",e);
            log.error("下载失败",e);
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("数据删除(单个)")
    public R<Boolean>  del(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        return R.ok(gpsTargetService.delFile(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delBatch")
    @ApiOperation("数据删除(批量)")
    public R<Boolean>  delBath(@ApiParam(value = "id集合", name = "ids")@RequestBody List<Long> ids) {
        return R.ok(gpsTargetService.delBatch(ids));
    }


    /**
     * 新增 或 编辑
     */
    @PostMapping("/edit")
    @ApiOperation("数据新增/编辑")
    public R edit(@ApiParam @RequestBody GpsTargetInfoDetailVO vo) {
        gpsTargetService.edit(vo);
        return R.ok();
    }
}
